/*
 * @(#)Menu.java   09/09/21
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.gea.mainGUI;

import fr.ema.arch3d.gea.Evolution;
import fr.ema.arch3d.gea.mainGUI.fileDirectory.FileTree;
import fr.ema.arch3d.gea.mainGUI.listeners.CheckerMenuActionListener;
import fr.ema.arch3d.gea.mainGUI.listeners.FileMenuActionListener;
import fr.ema.arch3d.model.architecture.configuration.Configuration;
import fr.ema.arch3d.model.architecture.specification.Specification;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;

import org.objectweb.fractal.api.Component;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class Menu extends JMenuBar {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /** Field description */
    public JMenuItem behCMenuItem;

    /** Field description */
    public JMenu checkerMenu, fileMenu, evolveMenu;

    /** Field description */
    public JMenuItem newMenuItem, openMenuItem;

    /** Field description */
    public JMenuItem nameCMenuItem;

    /**
     * Constructs ...
     *
     */
    public Menu() {
        JRadioButtonMenuItem rbMenuItem;
        JCheckBoxMenuItem    cbMenuItem;

        // Build the first menu.
        fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_A);
        this.add(fileMenu);

        // a group of JMenuItems
        // add menuitem new
        newMenuItem = new JMenuItem("New", KeyEvent.VK_T);

        // menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
        newMenuItem.getAccessibleContext().setAccessibleDescription("Create new file ");
        fileMenu.add(newMenuItem);

        // add menuitem open
        openMenuItem = new JMenuItem("Open", KeyEvent.VK_T);

        // menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
//      menuItemOpen.setAccelerator(KeyStroke.getKeyStroke(
//      KeyEvent.VK_1, ActionEvent.ALT_MASK));
        openMenuItem.getAccessibleContext().setAccessibleDescription("Open a project directory");
        fileMenu.add(openMenuItem);

//      ImageIcon icon = createImageIcon("images/middle.gif");
//      menuItem1 = new JMenuItem("Open", icon);
//      menuItem1.setMnemonic(KeyEvent.VK_B);
//      menu1.add(menuItem1);
//      menuItem1 = new JMenuItem(icon);
//        newMenuItem.setMnemonic(KeyEvent.VK_D);
        fileMenu.add(newMenuItem);

        // Build Evolution Menu
        evolveMenu = new JMenu("Evolution");
        JMenu esMenu     = new JMenu("EvolutionSpecication");

        esMenu.setMnemonic(KeyEvent.VK_F);

        JMenuItem addcmpMenuItem = new JMenuItem("Add component type", KeyEvent.VK_N);
        JMenuItem dltcmpMenuItem = new JMenuItem("Delete component type", KeyEvent.VK_N);
        JMenuItem rplcmpMenuItem = new JMenuItem("Replae component type", KeyEvent.VK_N);

        esMenu.add(addcmpMenuItem);
        esMenu.add(dltcmpMenuItem);
        esMenu.add(rplcmpMenuItem);
        evolveMenu.add(esMenu);

        JMenu ecMenu = new JMenu("EvolutionConfiguration");

        esMenu.setMnemonic(KeyEvent.VK_F);

        JMenuItem addcmpcMenuItem = new JMenuItem("Add component class", KeyEvent.VK_N);
        JMenuItem dltcmpcMenuItem = new JMenuItem("Delete component class", KeyEvent.VK_N);
        JMenuItem rplcmpcMenuItem = new JMenuItem("Replae component class", KeyEvent.VK_N);

        ecMenu.add(addcmpcMenuItem);
        ecMenu.add(dltcmpcMenuItem);
        ecMenu.add(rplcmpcMenuItem);
        evolveMenu.add(ecMenu);

        JMenu eaMenu = new JMenu("EvolutionAssembly");

        esMenu.setMnemonic(KeyEvent.VK_F);

        JMenuItem addcmpaMenuItem = new JMenuItem("Add component instance", KeyEvent.VK_N);
        JMenuItem dltcmpaMenuItem = new JMenuItem("Delete component instance", KeyEvent.VK_N);
        JMenuItem rplcmpaMenuItem = new JMenuItem("Replae component instance", KeyEvent.VK_N);

        eaMenu.add(addcmpaMenuItem);
        eaMenu.add(dltcmpaMenuItem);
        eaMenu.add(rplcmpaMenuItem);
        evolveMenu.add(eaMenu);

        // Add evolution menu to menuBar
        this.add(evolveMenu);

        // add checker menu to menuBar
        this.add(this.getCheckerMenu());
        rplcmpaMenuItem.addMouseListener(new MouseListener() {
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {

                // TODO Auto-generated method stub
            }
            public void mousePressed(MouseEvent e) {
                JFrame frame = new JFrame("Component Instance substitution");

                // frame.getContentPane().add(pane);
                // frame.pack();
                // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
            public void mouseReleased(MouseEvent e) {

                // TODO Auto-generated method stub
            }
            public void mouseClicked(MouseEvent e) {}
        });
    }

    /**
     * Method description
     *
     * @return  
     */
    private JMenu getCheckerMenu() {
        if (checkerMenu == null) {
            checkerMenu = new JMenu();
            checkerMenu.setText("Checker");
            checkerMenu.setOpaque(false);
            checkerMenu.add(getNameCMenuItem());
            checkerMenu.add(getBehCMenuItem());
        }

        return checkerMenu;
    }

    /**
     * Method description
     *
     * @return  
     */
    private JMenuItem getNameCMenuItem() {
        if (nameCMenuItem == null) {
            nameCMenuItem = new JMenuItem();
            nameCMenuItem.setText("Check Name");
        }

        return nameCMenuItem;
    }

    /**
     * Method description
     *
     * @return  
     */
    private JMenuItem getBehCMenuItem() {
        if (behCMenuItem == null) {
            behCMenuItem = new JMenuItem();
            behCMenuItem.setText("Check Behavior");
        }

        return behCMenuItem;
    }

    /**
     * Returns an ImageIcon, or null if the path was invalid. 
     * @param path  
     * @return  
     */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Evolution.class.getResource(path);

        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);

            return null;
        }
    }




    /**
     * Method description
     *
     * @param fc
     * @param viewTabbedPane
     * @param editorTabbedPane
     * @param fileTree
     * @param fileTreePanel
     */
    public void addActionListener(JFileChooser fc, JTabbedPane viewTabbedPane,
	    JTabbedPane editorTabbedPane, FileTree fileTree,
	    JComponent fileTreePanel) {
	openMenuItem.addActionListener(new FileMenuActionListener(fc, viewTabbedPane, editorTabbedPane, fileTree,
                fileTreePanel));
	newMenuItem.addActionListener(new FileMenuActionListener(fc, viewTabbedPane, editorTabbedPane, fileTree,
                fileTreePanel));
	
    }
    
    public void addActionListener(JTabbedPane viewTabbedPane,
	    Specification spec, Configuration config, Component container) {
	nameCMenuItem.addActionListener(new CheckerMenuActionListener(viewTabbedPane,
	    spec, config, container));
	behCMenuItem.addActionListener(new CheckerMenuActionListener(viewTabbedPane,
		    spec, config, container));
    }
    
}
